<?php
	
	function run_query($sql) {
		echo "runningme $sql<br/>";
		$result = mysql_query($sql) or die (mysql_error());
		$count = mysql_num_rows($result);
		$final = array("data" => $result, "count" => $count );
		return $final;
	}
	
	
	function upload_picture($gambarnya){
		
	} //returns picture name!
	
	function create_group($group_name, $group_parent, $group_admin, $group_permission, $group_request, $group_hidden, $group_picture_name) {
		$sql = "INSERT INTO groups VALUE('', '$group_name', $group_parent, $group_admin, $group_permission, $group_request, $group_hidden, '$group_picture_name')";
		mysql_query($sql) or die (mysql_error());
		return 1; //success
	}
	
	function delete_customtabledata($tableid){
		$sql =  "DELETE FROM customtabledata WHERE tableid = '$tableid'; ";
		run_query($sql);
		echo "delete data from table id $tableid</br>";
	}
	
	function delete_customtable($group_id){
		$sql =  "SELECT `id` FROM `customtable` WHERE groupid = '$group_id';";
		$result = run_query($sql);
		if($result["count"] > 0) {
			while($row = mysql_fetch_array($result["data"])){
				delete_customtabledata($row[0]);
				echo "delete table from table id $group_id</br>";
				$sql2= "DELETE FROM customtable WHERE id = $row[0];";
				run_query($sql2);
			}
		}
	}
		
	function delete_group($group_id){
		$sql =  "SELECT `id` FROM `group` WHERE parent = '$group_id';";
		$result = run_query($sql);
		if($result["count"] > 0) {
			while($row = mysql_fetch_array($result["data"])){
				delete_group($row[0]);
			}
		}
		$sql = "DELETE FROM `groupwall` WHERE groupid = '$group_id'; ";
		mysql_query($sql) or die(mysql_error());
		delete_customtable($group_id);
		$sql = "DELETE  FROM  `membership` WHERE groupid = '$group_id'; ";
		mysql_query($sql) or die(mysql_error());
		$sql = "DELETE  FROM `group` WHERE `id` = '$group_id'; ";
		mysql_query($sql) or die(mysql_error());
		echo "delete group  $group_id</br>";
	}
?>